import global, { Json_skill } from "../global";
import Storage from "../util/storage";
import Item_skill from "./item_skill";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Library extends cc.Component {

    @property(cc.Sprite)
    Arr_mySkills:cc.Sprite[] = [];      //我携带的四个技能

    @property(cc.Sprite)
    Skill_sp:cc.Sprite = null;          //选中技能的图片
    @property(cc.Label)
    Skill_name:cc.Label = null;         //技能名称
    @property(cc.Label)
    Skill_type:cc.Label = null;         //技能类型
    @property(cc.Label)
    Skill_desc:cc.Label = null;         //技能描述
    @property(cc.Label)
    Skill_cost:cc.Label = null;         //技能消耗
    @property(cc.Label)
    Skill_time:cc.Label = null;         //技能耐久
    @property(cc.Label)
    Skill_hold:cc.Label = null;         //技能充能
    @property(cc.Label)
    Skill_cooling:cc.Label = null;      //技能冷却

    @property(cc.Button)
    Button:cc.Button = null;            //领悟按钮

    @property(cc.Node)
    Content:cc.Node = null;             //容器
    @property(cc.Prefab)
    Prefab_item:cc.Prefab = null;       //技能item

    onLoad(){
        global.library = this;
        this.flush();
        this.display();
        this.show(global.temp.arrSkill[0]);
    }
    
    /**
     * 携带技能展示
     */
    flush(){
        let userSkill = global.data.userSkill;
        global.loadSf(this.Arr_mySkills[0],`/skill/${userSkill.skill1}`);
        global.loadSf(this.Arr_mySkills[1],`/skill/${userSkill.skill2}`);
        global.loadSf(this.Arr_mySkills[2],`/skill/${userSkill.skill3}`);
        global.loadSf(this.Arr_mySkills[3],`/skill/${userSkill.skill4}`);
    }

    /**
     * 全部技能展示
     */
    display(){
        global.temp.arrSkill.forEach(json_skill=>{
            let node = cc.instantiate(this.Prefab_item);
            node.getComponent(Item_skill).init(json_skill);
            this.Content.addChild(node);
        });
    }

    /**
     * 选中技能展示
     */
    now_show:Json_skill = null;
    show(json_skill:Json_skill){
        this.now_show = json_skill;
        this.Skill_name.string = json_skill.name;
        this.Skill_type.string = json_skill.type;
        this.Skill_desc.string = json_skill.desc;
        this.Skill_cost.string = "激活："+json_skill.cost;
        this.Skill_time.string = "耐久："+json_skill.time;
        this.Skill_cooling.string = "冷却："+json_skill.cooling;
        this.Skill_hold.string = "充能："+json_skill.hold;
        global.loadSf(this.Skill_sp,`/skill/${json_skill.bh}`);
        //如果这个技能我已经学会了,而且是四号位，就隐藏领悟按钮
        if(global.data.userSkill.skill4==json_skill.bh){
            this.Button.interactable = false;
        }else{
            this.Button.interactable = true;
        }
    }

    /**
     * 领悟技能
     */
    study(){
        //所有技能顺位往前移 如果是1号位的技能
        if(global.data.userSkill.skill2==this.now_show.bh){
            global.data.userSkill.skill2 = global.data.userSkill.skill3;
            global.data.userSkill.skill3 = global.data.userSkill.skill4;
            global.data.userSkill.skill4 = this.now_show.bh;
        }else if(global.data.userSkill.skill3==this.now_show.bh){
            global.data.userSkill.skill3 = global.data.userSkill.skill4;
            global.data.userSkill.skill4 = this.now_show.bh;
        }else{
            global.data.userSkill.skill1 = global.data.userSkill.skill2;
            global.data.userSkill.skill2 = global.data.userSkill.skill3;
            global.data.userSkill.skill3 = global.data.userSkill.skill4;
            global.data.userSkill.skill4 = this.now_show.bh;
        }
        Storage.set("data",global.data);
        this.flush();
        this.Button.interactable = false;
    }

    /**
     * 返回
     */
    back(){
        cc.director.loadScene("index");
    }

}
